package GDoc::Job::Fetch::US::Legi::USCode;

use Moose;
use WWW::Mechanize;
use GDoc::Job;


extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://uscode.house.gov/pdf/2006/' );
}

sub run { 
    my $self = shift;
   
    if ( my $url = $self->data->{url} ) { 
        $self->_fetch_pdf( $url );
    } else { 
        print "fetching base" . $self->_baseurl . "\n";
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        my @links = $mech->find_all_links( url_regex => qr/\.pdf$/ );

        foreach my $link( @links ) { 
            my $job = GDoc::Job->new( queue => $self->fetch_queue,
                                      class => 'Fetch::US::Legi::USCode',
                                      data  => { url  => 'http://uscode.house.gov' . $link->url }
                                    );

            $job->submit;
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my $url = shift;

    my $mech = $self->_mech;

    print "fetching $url\n";

    $mech->get( $url );

    my ( $file ) = ( $url =~ /(\w+)\.pdf/ );
    $file .= '.pdf';
    my $key = 'us/legi/uscode/' . $file;

    print "storing $url -> $key\n";

    $self->store_pdf( key => $key, data => $mech->response->content );
}

1;


